package com.tax.common.sms;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
import com.tencentcloudapi.sms.v20190711.models.SendStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;



public class TxySmsUtil {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 腾讯云短信通道
     *
     * @param content
     * @param mobileAry
     * @return
     * @throws Exception
     */
    public  boolean txySmsSend(String content, String[] mobileAry) {
        try {



            String secretId = "AKIDylIXVFTwviExQv7ocFq2zuZGiuqcpqDL";
            //注入用户Id
            String secretKey = "2nIK7gRwBaDetrVsyIra3HJ4xaXEfrkR";
            //注入短信服务地址
            String sdkAppId = "1400423143";
            //注入key
            String sign = "海宁经开产业园";
            // 模板ID
            String templateId = "875479";

            Credential cred = new Credential(secretId, secretKey);

            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setReqMethod("GET");
            httpProfile.setConnTimeout(60);
            httpProfile.setEndpoint("sms.tencentcloudapi.com");

            ClientProfile clientProfile = new ClientProfile();
            //指定签名算法（默认为HmacSHA256）
            clientProfile.setSignMethod("HmacSHA256");
            clientProfile.setHttpProfile(httpProfile);
            SmsClient client = new SmsClient(cred, "", clientProfile);
            SendSmsRequest req = new SendSmsRequest();

            req.setSmsSdkAppid(sdkAppId);
            //签名内容
            req.setSign(sign);

            /* 国际/港澳台短信 senderid: 国内短信填空，默认未开通，如需开通请联系 [sms helper] */
            String senderid = "";
            req.setSenderId(senderid);

            /* 用户的 session 内容: 可以携带用户侧 ID 等上下文信息，server 会原样返回 */
            String session = "";
            req.setSessionContext(session);

            /* 短信码号扩展号: 默认未开通，如需开通请联系 [sms helper] */
            String extendcode = "";
            req.setExtendCode(extendcode);

            req.setTemplateID(templateId);

            /* 下发手机号码，采用 e.164 标准，+[国家或地区码][手机号]
             * 例如+8613711112222， 其中前面有一个+号 ，86为国家码，13711112222为手机号，最多不要超过200个手机号*/
            String[] phoneNumbers = new String[mobileAry.length];
            for (int i = 0; i < mobileAry.length; i++) {
                phoneNumbers[i] = "+86" + mobileAry[i];
            }
            req.setPhoneNumberSet(phoneNumbers);

            /* 模板参数: 若无模板参数，则设置为空*/
            String[] templateParams = {content};
            req.setTemplateParamSet(templateParams);

            /* 通过 client 对象调用 SendSms 方法发起请求。注意请求方法名与请求对象是对应的
             * 返回的 res 是一个 SendSmsResponse 类的实例，与请求对象对应 */
            SendSmsResponse res = client.SendSms(req);
            SendStatus[] sendStatusSet = res.getSendStatusSet();
            if (null != sendStatusSet && sendStatusSet.length > 0) {
                SendStatus sendStatus = sendStatusSet[0];
                logger.info("腾讯云短信推送结果:{}", sendStatus.getMessage());
            }

        } catch (Exception e) {
            logger.error("腾讯云短信推送失败", e);
        }
        return true;
    }



}
